package ptaweek2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Topic02 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		String[] ids = new String[100];
		for(int i=0;i<n;i++){
			ids[i] = sc.next();
		}
		ArrayList str = new ArrayList(); 
		while(true){
			
			String ch = sc.next();
			while(ch.equals("sort1")||ch.equals("sort2")){
				str.add(ch);
				ch = sc.next();
			}
			for(int r=0;r<str.size();r++){
					
				switch((String)str.get(r)){
				case "sort1":
					String[] year = new String[n];
					String[] month = new String[n];
					String[] day = new String[n];
					String[] data1 = new String[n];
					for(int i=0;i<n;i++){
					year[i] = ids[i].substring(6, 10);
					month[i] = ids[i].substring(10, 12);
					day[i] = ids[i].substring(12, 14);
					data1[i] = year[i]+"-"+month[i]+"-"+day[i];
					}
					Arrays.sort(data1);
					for(int i=0;i<n;i++){
						System.out.println(data1[i]);
					}
				    break;
				    
				    
				case "sort2":
					String[] data2 = new String[n];
					for(int i=0;i<n;i++){
						data2[i] = ids[i].substring(6, 14);
						
					}
					for(int i=0;i<n;i++){
						for(int j=0;j<n-1;j++){
							int pre=Integer.parseInt(data2[j]);
							int latter=Integer.parseInt(data2[j+1]);
							if(pre>latter){
								String temp = data2[j];
								data2[j] = data2[j+1];
								data2[j+1] = temp;
								String temp2 = ids[j];
								ids[j] = ids[j+1];
								ids[j+1] = temp2;
								
							}
						}
					}
					for(int i=0;i<n;i++){
						System.out.println(ids[i]);
					}
					
					break;
				
				}
			}
			System.out.println("exit");
				
				
				
			
			
		}
		
			
			}
		}



